﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HomeWork
{
    public delegate string ContentFormatter(string title, string content);

    abstract class Content : Entity<int>  //内容类
    {

        protected Content Kind { get; set; }
        public DateTime CreateTime { get; }
        public String Title { get; set; }
        public String Body { get; set; } //内容       
        public User Author { get; set; } //作者

        // 封装空值检查逻辑
        protected void ValidateAuthorNotNull()
        {
            if (Author is null)
            {
                throw new ArgumentNullException(nameof(Author), "参数为空");
            }
        }

        public Content() 
        {
            CreateTime = DateTime.Now;
            Body = "写点东西吧"; //初始化内容为提示内容
        }

        public Content(int id,DateTime createTime) : base(id)
        {
            CreateTime = createTime; // 手动传入发布时间
            Body = "写点东西吧";
        }

        public Content(int id, DateTime createTime, User author) : base(id)
        {
            CreateTime = createTime;
            Author = author;
            Body = "写点东西吧";
            ValidateAuthorNotNull(); // 创建时就检查作者不为空
        }



        abstract public void Publish();

        // 新的 Publish 方法重载，接受一个 ContentFormatter 委托（或 lambda 表达式）

        public virtual void Publish(ContentFormatter formatter)
        {
            if (formatter == null)
            {
                // 可以提供默认行为或抛出异常
                Publish(); // 调用无参的默认版本
                return;
            }
            string formattedOutput = formatter(Title, Body);
            Console.WriteLine(formattedOutput);
        }

        //获取创建时间
        public DateTime GetPublishTime()
        {           
            return CreateTime;
        }


        public void SetKind(Content kind) //父类装子类
        {
            Kind = kind;
        }

        public void ifKind() //父类装子类
        {
            if (Kind is Suggest)
            {
                Console.WriteLine("建议类");
            }
            else if (Kind is Problem)
            {
                Console.WriteLine("悬赏类");
            }
            else if (Kind is Article)
            {
                Console.WriteLine("文章类");
            }

        }



    }

}
